/*=======================================================

    HCView V1.0
    文档页对象实现单元
    作者：荆通(18114532@qq.com)
    QQ群：649023932

=======================================================*/

import { TList } from "../hcl/System.js";
import { THCUnitConversion } from "./HCUnitConversion.js";
import { TPaperKind } from "./HCCommon.js";

export class THCPaper {
    constructor() {
        this.FSize = TPaperKind.A4;
        this.FWidth = 0;
        this.FHeight = 0;
        this.FWidthPix = 0;
        this.FHeightPix = 0;
        this.FMarginTop = 0;
        this.FMarginLeft = 0;
        this.FMarginRight = 0;
        this.FMarginBottom = 0;
        this.FMarginTopPix = 0;
        this.FMarginLeftPix = 0;
        this.FMarginRightPix = 0;
        this.FMarginBottomPix = 0;

        this.MarginLeft = 25;
        this.MarginTop = 25;
        this.MarginRight = 20;
        this.MarginBottom = 20;
        this.Size = TPaperKind.A4;
        this.Width = 210;
        this.Height = 297;
    }

    setSize_(val) {
        if (this.FSize != val)
            this.FSize = val;
    }

    setWidth_(val) {
        this.FWidth = val;
        this.FWidthPix = THCUnitConversion.millimeterToPixX(this.FWidth);
    }

    setHeight_(val) {
        this.FHeight = val;
        this.FHeightPix = THCUnitConversion.millimeterToPixY(this.FHeight);
    }

    setMarginTop_(val) {
        this.FMarginTop = val;
        this.FMarginTopPix = THCUnitConversion.millimeterToPixY(this.FMarginTop);
    }

    setMarginLeft_(val) {
        this.FMarginLeft = val;
        this.FMarginLeftPix = THCUnitConversion.millimeterToPixX(this.FMarginLeft);
    }

    setMarginRight_(val) {
        this.FMarginRight = val;
        this.FMarginRightPix = THCUnitConversion.millimeterToPixX(this.FMarginRight);
    }

    setMarginBottom_(val) {
        this.FMarginBottom = val;
        this.FMarginBottomPix = THCUnitConversion.millimeterToPixY(this.FMarginBottom);
    }

    SaveToStream(stream) {
        let vBegPos = stream.position;
        stream.writeUInt64(vBegPos);
        stream.writeInt32(this.FSize);
        stream.writeSingle(this.FWidth);
        stream.writeSingle(this.FHeight);
        stream.writeSingle(this.FMarginLeft);
        stream.writeSingle(this.FMarginTop);
        stream.writeSingle(this.FMarginRight);
        stream.writeSingle(this.FMarginBottom);

        let vEndPos = stream.position;
        stream.position = vBegPos;
        vBegPos = vEndPos - vBegPos - 8;

        stream.writeUInt64(vBegPos);
        stream.position = vEndPos;
    }

    LoadFromStream(stream, fileVersion) {
        stream.readInt64();  // let vDataSize = 
        this.Size = stream.readInt32();
        this.Width = stream.readSingle();
        this.Height = stream.readSingle();
        this.MarginLeft = stream.readSingle();
        this.MarginTop = stream.readSingle();
        this.MarginRight = stream.readSingle();
        this.MarginBottom = stream.readSingle();
    }

    get Size() {
        return this.FSize;
    }

    set Size(val) {
        this.setSize_(val);
    }

    get Width() {
        return this.FWidth;
    }

    set Width(val) {
        this.setWidth_(val);
    }

    get Height() {
        return this.FHeight;
    }

    set Height(val) {
        this.setHeight_(val);
    }

    get MarginTop() {
        return this.FMarginTop;
    }

    set MarginTop(val) {
        this.setMarginTop_(val);
    }

    get MarginLeft() {
        return this.FMarginLeft;
    }

    set MarginLeft(val) {
        this.setMarginLeft_(val);
    }

    get MarginRight() {
        return this.FMarginRight;
    }

    set MarginRight(val) {
        this.setMarginRight_(val);
    }

    get MarginBottom() {
        return this.FMarginBottom;
    }

    set MarginBottom(val) {
        this.setMarginBottom_(val);
    }

    get WidthPix() {
        return this.FWidthPix;
    }

    get HeightPix() {
        return this.FHeightPix;
    }

    get MarginTopPix() {
        return this.FMarginTopPix;
    }

    get MarginLeftPix() {
        return this.FMarginLeftPix;
    }

    get MarginRightPix() {
        return this.FMarginRightPix;
    }

    get MarginBottomPix() {
        return this.FMarginBottomPix;
    }
}

export class THCPage {
    constructor() {
        this.Clear();
    }

    Assign(src) {
        this.FStartDrawItemNo = src.StartDrawItemNo;
        this.FEndDrawItemNo = src.EndDrawItemNo;
    }

    Clear() {
        this.FStartDrawItemNo = 0;
        this.FEndDrawItemNo = 0;
    }

    get StartDrawItemNo() {
        return this.FStartDrawItemNo;
    }

    set StartDrawItemNo(val) {
        this.FStartDrawItemNo = val;
    }

    get EndDrawItemNo() {
        return this.FEndDrawItemNo;
    }

    set EndDrawItemNo(val) {
        this.FEndDrawItemNo = val;
    }
}

export class THCPages extends TList {
    constructor() {
        super();
    }

    ClearEx() {
        this.removeRange(1, this.count - 1);
        this[0].Clear();
    }
}